package serviceimpl;
import dao.AdminDao;
import dao.UserDao;
import daoimpl.AdminDaoImpl;
import daoimpl.UserDaoImpl;
import javabean.Admin;
import javabean.User;
import service.MainModuleService;

import java.util.ArrayList;

public class MainModuleServiceImpl implements MainModuleService {
    //创建服务需要操作数据库的dao对象
    private final UserDao<User> userDao = new UserDaoImpl();
    private final AdminDao<Admin> adminDao = new AdminDaoImpl();


    @Override
    public User userLogin(String username, String password) {
        ArrayList<User> users = userDao.selectByUsernameAndPassword(username, password);
        if (!users.isEmpty()) {
            return users.get(0);
        }
        return null;
    }

    @Override
    public Admin adminLogin(String username, String password) {
        ArrayList<Admin> admins = adminDao.selectByUsernameAndPassword(username, password);
        if (!admins.isEmpty()) {
            return admins.get(0);
        }
        return null;
    }

    @Override
    public boolean userResetPassword(int uid, String password) {
        int i = userDao.updatePasswordById(uid, password);
        return i == 1;
    }

    @Override
    public boolean adminResetPassword(int aid, String password) {
        int i = adminDao.updatePasswordById(aid, password);
        return i == 1;
    }
}